<script setup>
import { ref } from 'vue'
import { useUserInfoStore } from '@/stores/userInfo.js'
const userInfoStore = useUserInfoStore()
const userPwd = ref({
    old_pwd: '',
    new_pwd: '',
    re_pwd: ''
})

// 自定义确认密码校验规则
const rePasswordValid = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请输入确认密码'))
    } else if (value !== userPwd.value.new_pwd) {
        callback(new Error("两次密码输入不一致!"))
    } else {
        callback()
    }
}

const rules = {
    old_pwd: [
        { required: true, message: '请输入旧密码', trigger: 'blur' }
    ],
    new_pwd: [
        { required: true, message: '请输入新密码', trigger: 'blur' },
        { min: 5, max: 16, message: '长度在5-16位', trigger: 'blur' }
    ],
    re_pwd: [
        { validator: rePasswordValid, trigger: 'blur' }
    ]
}

import { userPwdUpdateService } from '@/api/user.js'
import { ElMessage } from "element-plus";
const updateUserInfo = async () => {
    let result = await userPwdUpdateService(userPwd.value)
    ElMessage.success(result.message ? result.message : '修改密码成功')
}
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>重置密码</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="userPwd" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="旧密码" prop="old_pwd">
                        <el-input v-model="userPwd.old_pwd" type="password"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="new_pwd">
                        <el-input v-model="userPwd.new_pwd" type="password"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码确认" prop="re_pwd">
                        <el-input v-model="userPwd.re_pwd" type="password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updateUserInfo">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>